<?php
/**
 * CK框架 分页类
 * @author wujibing<283109896@qq.com>
 * @version   3.0
 * @category   CkFrameWork
 * @package  CkFrameWork
 * @subpackage lib
 */
class page
{

	/**
	 * 总数量
	 * @var int
	 */
	private static $_total = 0;

	/**
	 * 每页数量
	 * @var int
	 */
	private static $_pageNum = 20;

	/**
	 * 当前页
	 * @var int
	 */
	private static $_nowPage = 0;

	/**
	 * 错误原因
	 * @var string
	 */
	private static $_error = '';

	/**
	 * 最大页数
	 * @var unknown_type
	 */
	private static $_maxPage = 0;

	/**
	 * 设置总数量
	 * @param int $total
	 */
	public static function setTotal ($total)
	{
		$total = (int) $total;
		if ($total >= 0)
		{
			self::$_total = $total;
		}
	}

	/**
	 * 设置每页数量
	 * @param int $num
	 */
	public static function setPageNum ($num)
	{
		$num = (int) $num;
		$num > 0 ? '' : $num = 20;
		self::$_pageNum = $num;
	}

	/**
	 * 获取每页数量
	 * @return int
	 */
	public static function getPageNum ()
	{
		return self::$_pageNum;
	}

	/**
	 * 获取最大页数
	 * @return int
	 */
	public static function getMaxPage ()
	{
		return self::$_maxPage;
	}

	/**
	 * 获取当前页
	 * @return int
	 */
	public static function getNowPage ()
	{
		return self::$_nowPage;
	}

	/**
	 * 获取分页数组
	 * @param int $nowPage 当前页
	 * @param int $total 总数量
	 * @param int $pageNum 一页数量
	 * @throws nowPageLtMaxPage_page_Exception
	 * @return array
	 */
	public static function getPage ($nowPage, $total = 0, $pageNum = 0)
	{
		if ($total)
		{
			self::setTotal($total);
		}
		if ($pageNum)
		{
			self::setPageNum($pageNum);
		}
		$nowPage = (int) $nowPage;
		$nowPage < 1 ? $nowPage = 1 : '';
		self::$_nowPage = $nowPage;
		$maxPage = self::$_maxPage = ceil(self::$_total / self::$_pageNum);
		if (! $maxPage)
		{
			return false;
		}
		if ($nowPage > $maxPage)
		{
			throw new nowPageLtMaxPage_page_Exception('');
		}
		$dataArray = array();
		$dataArray['第一页'] = 1;
		if ($nowPage > 1)
		{
			$dataArray['上一页'] = $nowPage - 1;
		}
		
		$dataArray['1'] = 1;
		
		if ($nowPage > 5)
		{
			for ($i = $nowPage - 5; $i < $nowPage; $i ++)
			{
				if ($i <= 0)
					continue;
				$dataArray[$i] = $i;
			}
			$dataArray[$nowPage] = $nowPage;
			for ($i = 0; $i < 5; $i ++)
			{
				$p = $nowPage + $i;
				if ($p > $maxPage)
				{
					break;
				}
				$dataArray[$p] = $p;
			}
		} else
		{
			
			for ($i = 1; $i < 10; $i ++)
			{
				if ($i > $maxPage)
				{
					break;
				}
				$dataArray[$i] = $i;
			}
		}
		$nextPage = $nowPage + 1;
		
		if ($nowPage < $maxPage)
		{
			$dataArray['下一页'] = $nextPage;
		}
		
		$dataArray['末页'] = $maxPage;
		
		return $dataArray;
	}
}
